import logging


from queue import Queue

logger = logging.getLogger(__name__)

class Communication:
    """ 检测车位的变化, 并将结果传递给外部"""

    def __init__(self):
        # channel 处理图像的的队列,长度为2, 用于存储图像的信息
        # 当前停车的对象列表
        self.parking_list = []  #: list[ParkingObject]

    def parking_change(self, data):
        """ 发送最新的检测信息,通过算法检测停车位变化 """
        set1 = set(i.name for i in self.parking_list)
        set2 = set(i.name for i in data)
        leval = set1 - set2
        add = set2 - set1
        # 返回结果
        res = {"enter": [], "leave": []}
        for item in leval:
            for i in data:
                if i.name == item:
                    res["leave"].append(i.serialize())
                    logger.info("车辆离开了停车位{}".format(item))
        for item in add:
            for i in data:
                if i.name == item:
                    res["enter"].append(i.serialize())
                    logger.info("车辆驶入了停车位{}".format(item))

        self.parking_list = data

        self.send_massage(res)

    def send_massage(self, msg):
        # 向外界发送消息, 通知车位的变化
        # todo 跟智慧停车进行交互
        print("向外界发送消息", msg)
        print('\n\n')

